$(function() {
	initRolesTable();
});

initRolesTable = function() {
	$("#roles_table").dataTable().fnDestroy();
	$("#roles_table")
			.dataTable(
					{
						"processing" : true,
						"serverSide" : false,
						"bAutoWidth" : false, // 是否自适应宽度
						"paginationType" : "full_numbers",
						"ajax" : {
							"type" : "POST",
							"url" : "/Medical_Instrument/adminPost/getPostList",
						},
						"aaSorting" : [ [ 1, "asc" ] ],
						"aoColumnDefs" : [ {
							"orderable" : false,
							"aTargets" : [ 0, 2, 3, 4 ]
						} ],
						"createdRow" : function(row, data, index) {
							$('td', row).eq(0).html('').append(
									"<input type='checkbox' value='' name=''>");
							$('td', row)
									.eq(4)
									.html('')
									.append(
											"<a title='删除' href='javascript:;' onclick='admin_role_del(this,"
													+ "\""
													+ data.postId
													+ "\""
													+ ")\' "
													+ "class='ml-5' style='text-decoration:none'>删除</a>"
													+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a data-toggle='modal'"
													+ "data-target='#permissionList_modal'  onclick='getpermissionList("
													+ "\""
													+ data.postId
													+ "\""
													+ ")\' "
													+ "class='ml-5' style='text-decoration:none'>查看权限</a>");
						},
						"columns" : [ {
							"data" : "",
							"defaultContent" : ""
						}, {
							"data" : "postId",
							"defaultContent" : ""
						}, {
							"data" : "postChineseName",
							"defaultContent" : ""
						}, {
							"data" : "description",
							"defaultContent" : ""
						}, {
							"data" : "",
							"defaultContent" : ""
						} ]
					});
}

getpermissionList = function(postId) {
	$("#permissionList_table").dataTable().fnDestroy();
	$("#permissionList_table")
			.dataTable(
					{
						"processing" : true,
						"serverSide" : false,
						"bAutoWidth" : false, // 是否自适应宽度
						"paginationType" : "full_numbers",
						"ajax" : {
							"type" : "POST",
							"url" : "/Medical_Instrument/adminPermission/getPermissionListById",
							"data" : {
								"postId" : postId
							}
						},
						"aaSorting" : [ [ 0, "asc" ] ],
						"aoColumnDefs" : [ {
							"orderable" : false,
							"aTargets" : [ 0, 1, 2, 3, 4 ]
						} ],
						"createdRow" : function(row, data, index) {
							$('td', row)
									.eq(4)
									.html('')
									.append(
											"<a title='删除' href='javascript:;' onclick='admin_PostPermission_del(this,"
													+ "\""
													+ data.permissionId
													+ "\",\""
													+ postId
													+ "\""
													+ ")\' "
													+ "class='ml-5' style='text-decoration:none'>删除</a>");
						},
						"columns" : [ {
							"data" : "permissionId",
							"defaultContent" : ""
						}, {
							"data" : "permissionUrl",
							"defaultContent" : ""
						}, {
							"data" : "permissionName",
							"defaultContent" : ""
						}, {
							"data" : "permissionDescription",
							"defaultContent" : ""
						}, {
							"data" : "",
							"defaultContent" : ""
						} ]
					});
}

/* 管理员-权限-删除 */
function admin_PostPermission_del(obj, permissionId, postId) {
	$(obj).parent("tr").siblings().removeClass("selected");
	$(obj).parent("tr").addClass('selected');

	layer.confirm('是否删除该角色下的该权限？', function(index) {
		$.ajax({
			type : 'POST',
			url : '/Medical_Instrument/adminPostPermission/delPostPermission',
			data : {
				"permissionId" : permissionId,
				"postId" : postId
			},
			dataType : 'json',
			success : function(data) {
				if (data.result == "success") {
					var table = $('#permissionList_table').DataTable();
					table.row('.selected').remove().draw(false);
					layer.msg('已删除!', {
						icon : 1,
						time : 1000
					});
				}
			},
			error : function(data) {
				console.log(data.msg);
			},
		});
	});
}

$('#permissionList_table tbody').on('click', 'tr', function() {
	if ($(this).hasClass('selected')) {
		$(this).removeClass('selected');
	} else {
		$('#permissionList_table tr.selected').removeClass('selected');
		$(this).addClass('selected');
	}
});
